
import { EntityManager, MikroORM } from '@mikro-orm/mysql';
import { InjectEntityManager, InjectMikroORM } from '@mikro-orm/nestjs';
import { Controller, Get } from '@nestjs/common';
import { Users } from "@entities/Users";
import { UserskVO } from "@databases/vo/UserVO";
import { knexPrefix } from "@databases/vo/dao-helper";
import { LoginRecoders } from "@entities/LoginRecoders";
import { JsonApiResponse } from '@utils/json.api.response';
import { ApiTags } from '@nestjs/swagger';
import { knext } from 'knext';
 
@ApiTags('(站点) 登录')
@Controller('login')
export class LoginController {
    constructor(
        @InjectMikroORM('zp')
        private readonly orm : MikroORM,
        @InjectEntityManager('zp')
        private readonly em : EntityManager    
    ){}
    

    @Get('record/list')
    async list()
    {
        const knex = knext(this)

        const ids = await knex
        .fromORM(LoginRecoders)
        .pluck('user_id')
        .orderBy('times','desc')
        .limit(5)
        .populate()


        const users = await knex
        .fromORM(Users)
        .select(
            ...knexPrefix(UserskVO.baseInfo())
        )
        .whereIn('id',ids)
        .populate()
         
        return JsonApiResponse.new(users)
    }
}
